package fenae.pesquisa;

import java.sql.Connection;
import java.sql.PreparedStatement;

import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.util.ITransaction;
import lumis.util.PortalUtil;

public class RespostaUsuarioDaoJdbc {
	public void registraRespostaObjetivaUsuario(String pesquisauid,
			String perguntauid, String respostauid, String usuariouid,
			ITransaction transaction) throws PortalException {
		try {
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
			String respostausuariouid = PortalUtil.generateNewGuid();

			PreparedStatement statement = connection
					.prepareStatement("insert into respostaUsuario (respostausuariouid, usuariouid, pesquisauid, perguntauid, respostauid)  values (?, ?, ?, ?, ?)");
			try {
				statement.setString(1, respostausuariouid);
				statement.setString(2, usuariouid);
				statement.setString(3, pesquisauid);
				statement.setString(4, perguntauid);
				statement.setString(5, respostauid);
				statement.executeUpdate();
			} finally {
				statement.close();
			}
		} catch (PortalException e) {
			throw e;
		} catch (Exception e) {
			throw new UnexpectedException(e);
		}
	}

	public void registraRespostaDiscursivaUsuario(String pesquisauid,
			String perguntauid, String respostaDiscursiva, String usuariouid,
			ITransaction transaction) throws PortalException {
		try {
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
			String respostausuariouid = PortalUtil.generateNewGuid();

			PreparedStatement statement = connection
					.prepareStatement("insert into respostaUsuario (respostausuariouid, usuariouid, pesquisauid, perguntauid, resposta)  values (?, ?, ?, ?, ?)");
			try {
				statement.setString(1, respostausuariouid);
				statement.setString(2, usuariouid);
				statement.setString(3, pesquisauid);
				statement.setString(4, perguntauid);
				statement.setString(5, respostaDiscursiva);
				statement.executeUpdate();
			} finally {
				statement.close();
			}
		} catch (PortalException e) {
			throw e;
		} catch (Exception e) {
			throw new UnexpectedException(e);
		}
	}
}
